<template>
    <div>
        <div class="job">
            <el-form :model="jobForm" label-width="80px" size="medium" style="margin-left:300px;margin-top:10px">
                <el-form-item label="职位名称" style="width:600px">
                    <el-input v-model="jobForm.job_title" :disabled="ableToUpdate"></el-input>
                </el-form-item>
                <el-form-item label="职位分类" style="width:600px">
                    <el-select v-model="jobForm.job_classification" placeholder="职位分类" :disabled="ableToUpdate">
                        <el-option v-for="job_classification in job_classification_list"
                            :key="job_classification.job_classification_id" :value="job_classification.job_classification"
                            :label="job_classification.job_classification"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="薪资" style="width:600px">
                    <el-input v-model="jobForm.salary" :disabled="ableToUpdate"></el-input>
                </el-form-item>
                <el-form-item label="职位要求" style="width:600px;">
                    <el-input v-model="jobForm.job_requirements" type="textarea" :autosize="{ minRows: 5, maxRows: 40 }"
                        :disabled="ableToUpdate"></el-input>
                </el-form-item>
                <el-form-item label="职位待遇" style="width:600px">
                    <el-input v-model="jobForm.position_treatment" type="textarea" :autosize="{ minRows: 5, maxRows: 40 }"
                        :disabled="ableToUpdate"></el-input>
                </el-form-item>
                <el-form-item label="公司简介" style="width:600px">
                    <el-input v-model="jobForm.company_profile" :disabled="ableToUpdate" type="textarea"
                        :autosize="{ minRows: 5, maxRows: 40 }">
                    </el-input>
                </el-form-item>
                <el-button type="primary" @click="toUpdateJob" style="margin-left:-400px;" v-show="!isUpdate">修改</el-button>
                <el-button type="primary" @click="doUpdateJob(jobForm)" v-show="isUpdate"
                    style="margin-left:-420px;">提交修改</el-button>
                <el-button type="primary" @click="doNotUpdateJob" v-show="isUpdate"
                    style="margin-left:-440px;">返回</el-button>
            </el-form>
        </div>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            isUpdate: false,
            ableToUpdate: true,
            job_classification_list: [],
            jobForm: {
                job_title: '',
                job_classification: '',
                enterprise_name: '',
                job_requirements: '',
                salary: '',
                cover: '',
                enterprise_number: '',
                position_treatment: '',
                company_profile: '',
            }
        };
    },
    methods: {
        doNotUpdateJob() {
            window.location.reload();
        },
        toUpdateJob() {
            this.ableToUpdate = false;
            this.isUpdate = true;
        },
        doUpdateJob(jobForm) {
            if (!this.jobForm.job_title) {
                this.$message.error("请输入职位名称!");
                return false;
            }
            if (!this.jobForm.job_classification) {
                this.$message.error("请选择职位分类!");
                return false;
            }
            if (!this.jobForm.salary) {
                this.$message.error("请输入薪资!");
                return false;
            }
            if (!this.jobForm.job_requirements) {
                this.$message.error("请输入职位要求!");
                return false;
            }
            if (!this.jobForm.position_treatment) {
                this.$message.error("请输入职位待遇!");
                return false;
            }
            if (!this.jobForm.company_profile) {
                this.$message.error("请输入公司简介!");
                return false;
            }
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/job_recruitment/set?job_recruitment_id=' + window.localStorage.getItem("job_recruitment_id"),
                data: {
                    job_title: jobForm.job_title,
                    job_classification: jobForm.job_classification,
                    job_requirements: jobForm.job_requirements,
                    salary: jobForm.salary,
                    position_treatment: jobForm.position_treatment,
                    company_profile: jobForm.company_profile,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.$message.success("更新职位信息成功");
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
    },
    beforeMount: function () {
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/job_classification/get_list',
            params: {

            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                that.job_classification_list = response.data.result.list;
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    },
    created: function () {
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/get_obj',
            params: {
                user_id: window.localStorage.getItem("user_id")
            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                if (!response.data.result) {
                    that.$message.error("您还没有完善企业信息!请去完善企业信息!!!");
                    that.$router.push({ name: 'enterprise_information' }, () => { }, () => { });
                }
                that.jobForm.enterprise_name = response.data.result.obj.enterprise_name;
                that.jobForm.enterprise_number = response.data.result.obj.enterprise_number;
                let thats = that;
                //防止刷新时跳转到其它位置
                let id = that.$route.params.job_recruitment_id;
                if (!that.$route.params.job_recruitment_id) {
                    id = window.localStorage.getItem("job_recruitment_id");
                }
                axios({
                    method: 'get',
                    url: 'http://127.0.0.1:5000/api/job_recruitment/get_obj',
                    params: {
                        job_recruitment_id: id
                    },
                    headers: {
                        'X-Auth-Token': window.localStorage.getItem("token")
                    },
                })
                    .then(function (response) {
                        console.log(response.data);
                        thats.jobForm = response.data.result.obj;
                        //保存到本地仓库
                        window.localStorage.setItem("job_recruitment_id", response.data.result.obj.job_recruitment_id);
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
                    .then(function () {
                        // 总是会执行
                    })
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    }
};
</script>
<style lang="less" scoped>
.job {
    margin-top: -450px;
    margin-left: 500px;
}
</style>